package generator.util

import generator.common.{ExcelReader, Generator}
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.xssf.usermodel.XSSFSheet

import java.io.{File, FileInputStream}


class GenerateFromXSSFExcel[T](excelFileName:String,sheetIndex:Int,getDataSource: XSSFSheet=>T,sheetName:String) extends Generator[T] with ExcelReader {
  override var dataSource: T = getDataSource(readXSSFSheet(sheetIndex,excelFileName,sheetName))
  override def generate(op: T => Unit): Unit = op(dataSource)

  def setSheetName(name:String): Unit = {
    dataSource = getDataSource(readXSSFSheet(sheetIndex,excelFileName,name))
  }
}

object GenerateFromXSSFExcel {
  def apply[T](excelFileName: String, sheetIndex: Int, op: XSSFSheet => T,sheetName:String=""): GenerateFromXSSFExcel[T] = {
    new GenerateFromXSSFExcel(excelFileName, sheetIndex, op,sheetName) {
    }
  }
}
